import fileUpload from "@/components/file-upload.vue"
import propsButton from "@/components/propsButton.vue"
import {PayOptions} from "../constant"
import store from "@/store/index"
import {getOptions} from "@/apis/order/index"
import { addBill, getBillDetail, getBillOptions, updateAndStatus, updateBill } from "@/apis/bill"
import { getShopOPtions } from "@/apis/shop"
import {getToDay,getDateDetail} from "@/util/date"


async function callback(t){
	// debugger
	let id = t.$attrs.id || t.search?.form?.id
	if(t.$attrs.type=="edit" || id){
		let {data:billDetail} = await getBillDetail(id)
		t.tableData = billDetail.orderList
		t.search.form = {
			id:t.$attrs.id,
			billNo:billDetail.billNo,
			status:billDetail.status,
			shopId:billDetail.shopId,
			activeDate:billDetail.activeDate
		}
		return billDetail
	}else{
		let shopId = t.$attrs.shopId - 0 || ""
		return {
			billNo:"",
			status:0,
			shopId:shopId ,
			activeDate:getToDay()
		}
	}
	
}
function isEdit(t){
	return t.$attrs.type == "edit"
}
export default async (that)=>{
	console.log(that.$attrs.id)
	let billDetail = await callback(that)
	let {data:shopOptions} = await getShopOPtions()
	that.getList = ()=>{}
	return {
		columns:[
			{
				key:"expand",
				props:{
					type:"expand"
				},
				slot:"expand"
			},
			{
				key:"bid",
				label:"账单编号",
				width:200,
			},
			{
				key:"orderNo",
				label:"订单号",
				width:200,
			},
			{
				key:"money",
				label:"每单结算金额",
				width:120,
			},
			{
				key:"nickname",
				label:"微信昵称",
				// width:200,
			},
			{
				key:"phone",
				label:"手机尾号",
				// width:200,
			},
			{
				key:"amount",
				label:"实付金额",
				// width:200,
			},
			{
				key:"orderCreateTime",
				label:"订单创建时间",
				width:120,
			},
			// {
			// 	key:"createTime",
			// 	label:"创建时间",
			// 	width:200,
			// },
			// {
			// 	key:"updateTime",
			// 	label:"修改时间",
			// 	width:200,
			// },
			{
				key:"operation",
				label:"操作",
				slot:"operation",
				width:150,
				props:{
					fixed:"right"
				},
				removeBtn:{
					show:true,
					permission:"order:delete"
				},
				editBtn:{
					show:true,
					permission:"order:edit"
				},
			}
		],
		reqOptions:{
			insert:{
				method:"post",
				url:"/order",
				callback
			},
			update:{
				method:"put",
				url:"/order",
				callback
			},
			remove:{
				method:"delete",
				url:"/order/${id}",
				callback
			}
		},
		search:{
			form:{
				id:billDetail.id,
				billNo:billDetail.billNo,
				status:billDetail.status,
				shopId:billDetail.shopId,
				activeDate:billDetail.activeDate,
			},
			options:[
				{
					key:"shopId",
					type:"el-select-v2",
					label:"店铺",
					required:true,
					width:"1020px",
					props:{
						clearable:true,
						filterable:true,
						options:shopOptions,
					}
				},
				{
					key:"billNo",
					type:"el-input",
					label:"账单编号",
					required:true,
					props:{
						clearable:true
					}
    
				},
				{
					key:"activeDate",
					label:"活动日期",
					type:"el-date-picker",
					props:{
						type:"date",
						format:"yyyy-MM-dd",
						"value-format":"yyyy-MM-dd"
					},
					required:true
				},
				{
					key:"status",
					type:"el-select",
					label:"支付状态",
					permission:isEdit(that)?"bill:updateAndStatus":"",
					required:true,
					options:PayOptions
				},
				
				{
					key:"save",
					type:propsButton,
					ref:"saveButton",
					label:isEdit(that)?"保存":"添加",
					removeLabel:true,
					width:false,
					props:{
						type:"primary",
						// size:"medium"
					},
					listeners:{
						click(...e){
							that.searchFun((form)=>{
								if(isEdit(that)){
									if(store.state.permission.includes("bill:updateAndStatus")){
										updateAndStatus(form)
									}else{
										updateBill(form)
									}
								}else{
									if(!that.search.form.id){
										addBill(form).then(res=>{
											console.log(res)
											that.$refs.search.$refs.saveButton[0].text = "保存"
											that.search.form = res.data
											that.config.table.show = true
											that.config.operation.show = true
											// that.$router.push({

											// })
										})
									}else{
										updateBill(form)
									}
								}
								console.log("d")
							})
							
						}
					}
				},
				{
					key:"refrsh",
					type:"el-button",
					label:"刷新",
					removeLabel:true,
					width:false,
					props:{
						type:"warning",
						// size:"medium"
					},
					listeners:{
						async click(...e){
							console.log(that)
							callback(that)
							if(!isEdit(that)){
								that.search.form = {
									billNo:"",
									status:0,
								}
							}
						}
					}
				},
				{
					key:"goBill",
					type:"el-button",
					label:"返回",
					removeLabel:true,
					width:false,
					listeners:{
						click(){
							setTimeout(()=>{
								that.$router.back()
							})
						}
					}
				},
				
				
				// {
				// 	key:"test",
				// 	type:"el-button",
				// 	label:"查询",
				// 	removeLabel:true,
				// 	width:false,
				// 	props:{
				// 		type:"primary",
				// 		// size:"medium"
				// 	},
				// 	listeners:{
				// 		click(...e){
				// 			that.searchFun && that.searchFun instanceof Function && that.searchFun(...e)
				// 			that.$emit("search")
				// 			console.log("click")
				// 			that.getList()
				// 		}
				// 	}
				// },
			],
			direction:"h",
			labelPosition:"right",
			attrs:{
			}
		},
		operation:{
			form:{},
			direction:"h",
			options:[
				{
					key:"orderText",
					type:"el-link",
					label:"",
					slot:"orderText",
				},
				{
					key:"btn1",
					type:"el-button",
					label:"添加",
					width:false,
					removeLabel:true,
					props:{
						type:"primary",
						size:"mini",
						// icon:"el-icon-plus"
					},
					
					listeners:{
						click:async (...e)=>{							
							getOptions().then(res=>{
								console.log(res)
								store.commit("option/setBillOption",res.data)
							})
							console.log(that)
							// that.addFun && that.addFun instanceof Function && that.addFun(...e)
							that.updateField = await that.addFieldMethod({
								bid:that.search.form.billNo,
								orderCreateTime:getDateDetail()
							})
							that.mode = "add"
							that.show = true
						}
					}
				},
				{
					key:"batchChange",
					type:"el-button",
					label:"批量修改订单状态",
					slot:"batchChange",
					width:false,
					removeLabel:true,
					props:{
						type:"danger",
						size:"mini",
					},
				}
			]
		},
		updateField:async (form={bid:billDetail.billNo,orderCreateTime:getDateDetail()})=>{
			return {
				form,
				direction:"h",
				options:[
					// {
					// 	key:"bid",
					// 	label:"账单编号",
					// 	type:"el-select",
					// 	readStore:"billOption",
					// 	required:true
					// },
					{
						key:"bid",
						label:"账单编号",
						// type:"el-select",
						type:"el-select-v2",
						readStore:"billOption",
						required:true,
						props:{
							filterable:true,
							options:store.state.option.billOption
						}
					},
					{
						key:"orderId",
						label:"订单id",
						type:"el-input",
						required:true
					},
					{
						key:"orderNo",
						label:"订单号",
						type:"el-input",
						required:true
					},
					{
						key:"money",
						label:"每单结算金额",
						type:"el-input",
						required:true,
						props:{
							type:"number"
						}
					},
					{
						key:"amount",
						label:"实付金额",
						type:"el-input",
						required:true,
						props:{
							type:"number"
						}
					},
					{
						key:"nickname",
						label:"微信昵称",
						type:"el-input",
						// required:true
					},
					{
						key:"phone",
						label:"手机尾号",
						type:"el-input",
						// required:true
					},
					
					{
						key:"orderCreateTime",
						label:"订单创建时间",
						type:"el-date-picker",
						required:true,
						props:{
							type:"datetime",
							format:"yyyy-MM-dd HH:mm:ss",
							"value-format":"yyyy-MM-dd HH:mm:ss"
						},
					},
					{
						key:"imgList",
						label:"订单截图",
						type:fileUpload,
						props:{
							actionType:"images/order",
							imageField:"imgList",
							multiple:true
							
						},
						required:true,
						width:"500px",
					}
                    
				]
			}
		},
		config:{
			dialog:{
				top:"3vh"
			},
			table:{
				checkbox:true,
				autoHeight:false,
				show:isEdit(that)
			},
			operation:{
				show:isEdit(that)
			},
			pagination:{
				show:false
			}
		}
        
	}
}